package Z5;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.Random;

import javax.swing.*;

class Language {
	private int nr;
	private VetoableChangeSupport veto = new VetoableChangeSupport(this);

	public Language() {
		this.nr = 0;
	}

	void setNr(int newNr) throws PropertyVetoException {
		int old = this.nr;
		veto.fireVetoableChange("jezyk", old, newNr);
		this.nr = newNr;
	}

	public void addVetoableChangeListener(VetoableChangeListener l) {
		veto.addVetoableChangeListener(l);
	}

	public void removeVetoableChangeListener(VetoableChangeListener l) {
		veto.removeVetoableChangeListener(l);
	}
}

class TextView extends JLabel implements VetoableChangeListener {

	public TextView() {
		super("Ala ma kota");
		setPreferredSize(new Dimension(300, 40));
	}

	public void vetoableChange(PropertyChangeEvent evt)
			throws PropertyVetoException {
		int value = (Integer) evt.getNewValue();
		Random rnd = new Random();
		boolean b;
		
		switch (value) {
		case 0:
			setText("Ala ma kota");
			break;

		case 1:
			setText("Ala has a cat");
			break;

		case 2:
			setText("Ala hat eine Katze");
			break;

		case 3:
			b=rnd.nextBoolean();
			if(b)setText("Ala a un chat");
			else{
				JOptionPane jop = new JOptionPane();
				jop.showMessageDialog(null, "Zmiana sia nie udala");
				//throw new PropertyVetoException("Niepoprawna zmiana warto�ci", evt);
			}
			break;
		}

	}

}

@SuppressWarnings("serial")
class LanguageGUI extends JFrame implements ActionListener{
	
	Language lang;
	JButton pl, en, de, fr;
	
	LanguageGUI(Language l, TextView tv){
		this.lang=l;
		Container cp = getContentPane();
	    cp.setLayout(new FlowLayout());
	    pl=new JButton("PL");
	    en=new JButton("EN");
	    de=new JButton("DE");
	    fr=new JButton("FR");
	    pl.addActionListener(this);
	    en.addActionListener(this);
	    de.addActionListener(this);
	    fr.addActionListener(this);
	    cp.add(tv);
	    cp.add(pl);
	    cp.add(en);
	    cp.add(de);
	    cp.add(fr);
	    setDefaultCloseOperation(3);
	    pack();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
	      if (cmd.equals("PL"))
			try {
				lang.setNr(0);
			} catch (PropertyVetoException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		else if (cmd.equals("EN"))
			try {
				lang.setNr(1);
			} catch (PropertyVetoException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		else if (cmd.equals("DE"))
			try {
				lang.setNr(2);
			} catch (PropertyVetoException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		else if (cmd.equals("FR"))
			try {
				lang.setNr(3);
			} catch (PropertyVetoException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		else System.out.println("Unrecognized command");
		
	}
	
}

public class Zadanie1 {
	
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		Language lang = new Language();
		TextView tv = new TextView();
		lang.addVetoableChangeListener(tv);
		LanguageGUI gui = new LanguageGUI(lang, tv);
		gui.pack();
		gui.show();
	}
}
